package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.ProductType;
import cn.yj.cgs.dba.entity.vo.ProductVo;
import cn.yj.admin.frame.AbstractController;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.core.business.service.ProductService;
import cn.yj.entity.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-15 14:19
 */
@RestController
@RequestMapping("/product")
public class ProductController extends AbstractController<ProductVo> {
    @Resource
    private ProductService productService;

    @Override
    @GetMapping("/pageList")
    @RequiresPermissions("product:list")
    public R pageList(@RequestParam Map<String, Object> params) {
        return success(productService.pageList(params, page()));
    }

    @GetMapping("/getProductImg/{productNo}")
    @RequiresPermissions("product:list")
    public R getProductImg(@PathVariable String productNo) {
        return success(productService.selectProductImg(productNo));
    }

    @GetMapping("/type/pageList")
    @RequiresPermissions("product:type:list")
    public R typePageList(@RequestParam Map<String, Object> params) {
        return success(productService.typePageList(params, PageUtils.page()));
    }

    @GetMapping("/type/list")
    @RequiresPermissions("product:list")
    public R typeList() {
        return success(productService.typeList());
    }


    @PostMapping("/type/save")
    @RequiresPermissions("product:type:add")
    @OperateLog(describe = "添加二手商品类型",type = LogType.INSERT)
    public R save(@RequestBody ProductType productType) {
        return result(productService.saveAndUpdateType(productType));
    }

    @PutMapping("/type/update")
    @RequiresPermissions("product:type:update")
    @OperateLog(describe = "修改二手商品类型",type = LogType.UPDATE)
    public R update(@RequestBody ProductType productType) {
        return result(productService.saveAndUpdateType(productType));
    }

    @DeleteMapping("/type/delete/{typeNo}")
    @RequiresPermissions("product:type:del")
    @OperateLog(describe = "删除二手商品类型",type = LogType.DELETE)
    public R deleteType(@PathVariable("typeNo") String[] typeNo) {
        return result(productService.removeTypeByNo(Arrays.asList(typeNo)));
    }

    /**
     * 更新审核状态
     * @param body - status productNo
     * @return
     */
    @PutMapping("/update/auditStatus")
    @RequiresPermissions("product:update:audit:status")
    @OperateLog(describe = "物品审核",type = LogType.UPDATE)
    public R updateStatus(@RequestBody Map<String, Object> body){
        return result(productService.updateStatus(body));
    }



    @DeleteMapping("/delete/{productNo}")
    @OperateLog(describe = "删除二手商品类型")
    @RequiresPermissions("product:del")
    public R deleteProduct(@PathVariable("productNo") Integer[] productNo) {
        return result(productService.removeByNo(Arrays.asList(productNo)));
    }
}
